package com.wxq.workpad

import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.method.PasswordTransformationMethod
import android.view.View
import com.qianfan.qfcall.network.manager.HttpManager
import com.qianfan.qfcall.network.requestResponseThrowException
import com.samluys.statusbar.StatusBarUtils
import com.sum.network.error.ApiException
import com.wxq.workpad.accoutInfo.UserInfoManager
import com.wxq.workpad.apiservice.PadApiService
import com.wxq.workpad.base.BaseActivity
import com.wxq.workpad.base.lifeRecycle
import com.wxq.workpad.databinding.ActivityLoginBinding
import com.wxq.workpad.entity.parmer.LoginParmer
import com.wxq.workpad.lailiao.dialog.SetInputDialog
import com.wxq.workpad.upgradation.UpdateApkManager
import com.wxq.workpad.utils.log.LogUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

class LoginActivity : BaseActivity<ActivityLoginBinding>() {

var ispwdshow:Boolean=false
var isRemember:Boolean=true

    override fun initView(instanceState: Bundle?) {
        StatusBarUtils.transparencyBar(this@LoginActivity)
        StatusBarUtils.StatusBarIconDark(this@LoginActivity)

        binding.tvLogin.setOnClickListener{
            if (binding.etAccount.text.toString().equals("")) {
                showToast("请输入账号/手机号码")
                return@setOnClickListener
            }
            if (binding.etPwd.text.toString().equals("")) {
                showToast("请输入密码")
                return@setOnClickListener
            }

            GlobalScope.launch(Dispatchers.Main) {
                showLoading()
                try {
                    var parmer=    LoginParmer()
                    parmer.userName=binding.etAccount.text.toString()
                    parmer.password=binding.etPwd.text.toString()

                    var result= requestResponseThrowException {
                        HttpManager.create(PadApiService::class.java).login(parmer)
                    }
                    UserInfoManager.saveLoginInfo(parmer,result!!)
                    HomeActiviry.naveToActivity(mContext)
                    finish()
                } catch (e: Exception) {
                    if(e is ApiException){
                        showToast(e.errMsg)
                    }else {
                        showToast(e.toString())
                    }
                }
                dismissLoading()
            }.lifeRecycle(lifecycle)

        }


       binding.llRemberAccount.setOnClickListener{
           // 记住密码
           isRemember=!isRemember
           updateReemberType()
       }






        binding.ivPwdChange.setOnClickListener{
            ispwdshow=!ispwdshow
            updatePwdType()
        }

        if (BuildConfig.DEBUG){
            binding.tvDebug.visibility=View.VISIBLE
            binding.tvDebug.setOnClickListener {
//                FileExplorerActivity.startActivity(mContext)
//                UpdateApkManager.showInsatllDialog(mContext)

                SetInputDialog(mContext).show()


            }
        }



    }

    private fun updateReemberType() {
        if (isRemember) {
            binding.ivRememberPwd.setBackgroundResource(R.mipmap.ic_privacy_selected)

        }else{
            binding.ivRememberPwd.setBackgroundResource(R.mipmap.ic_privacy_noselected)
        }
    }

    private fun updatePwdType() {
        if (ispwdshow) {
            binding.ivPwdChange.setBackgroundResource(R.mipmap.canlook)
            binding.etPwd.transformationMethod= null
        }else{
            binding.ivPwdChange.setBackgroundResource(R.mipmap.nolook)
            binding.etPwd.transformationMethod= PasswordTransformationMethod.getInstance()
        }


    }

    override fun initData() {

    }




}